package sw6.xmlvalidator;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;

/**
 * This class enables one to check if the DTD referenced to in settings.xml
 * is a local copy or the onlinen version that sw6b provide. A user might
 * have downloaded and changed the DTD path to a local path, as this might
 * speed up the process of running this tool. Therefore, to remember the
 * user about that a newer DTD might exists, we provide this class. 
 * @author Lasse
 */
public class DTDLocator {

	/**
	 * Checks if the used DTD is the online version provided by sw6b.
	 * @param xmlFile A xml file using a DTD.
	 * @param onlineDtdPath The path to the online DTD.
	 * @return <code>true</code> if the DTD is placed online.
	 * <code>false</code> otherwise.
	 */
	public static int isDtdOnline(File xmlFile, String onlineDtdPath) {
		
		//Check if used DTD in settings.xml is the online version provided by sw6b
		System.out.print("Checking locality of DTD...");
		try {
			
			//Input streams are initialized
			FileReader fr 		= new FileReader(xmlFile);
			BufferedReader br 	= new BufferedReader(fr);
			String xmlFileLine 	= "";
			
			//The DTD info is fetched
			while((xmlFileLine = br.readLine()) != null) {
				
				//If the path to the online dtd is located, the online DTD is used
				if(xmlFileLine.contains(onlineDtdPath)) {
					System.out.println(" Online.");
					return 1;
				}
			}
		} catch(Exception e) {
			System.out.println(" Error.");
			return -1;
		}
		System.out.println(" Local.");
		return 0;
	}
}
